package android.test;

import network.NetworkManager;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import protocol.Reply;
import protocol.Request;

public class AndroidTestActivity extends Activity {
	
	private Button loginBtn;	
	private Button logoutBtn;	
	private Button searchAcountBtn;
	
	
	private EditText param1;
	private EditText param2;
	private EditText serverSays;
	
	private NetworkManager connect;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        
        connect = new NetworkManager("192.168.10.106", 80);
        
        setButtonInit();
        
        param1 = (EditText)findViewById(R.id.param1);
        param2 = (EditText)findViewById(R.id.param2);
        serverSays = (EditText)findViewById(R.id.serversays);
         
    }
    
    
    
    private void setButtonInit() {		
    	
    	loginBtn = (Button)findViewById(R.id.btn_login);
    	loginBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				procLogin();
			}
		});
    	
    	logoutBtn = (Button)findViewById(R.id.btn_logout);
    	logoutBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				procLogout();
			}
		});
		
    	searchAcountBtn = (Button)findViewById(R.id.btn_search_acount);
    	searchAcountBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				procSearchAcount();
			}
		});		
	}



	/**
	 * <b>메소드 :</b>
	 * <b>메소드 한글/영문 명을 여기에 기입합니다.</b>
	 * <p>
	 * <ul>
	 * <li>메소드에 대한 설명1
	 * <li>메소드에 대한 설명2
	 * </ul>
	 * </p>
	 * @since 2012. 6. 3.
	 */
	protected void procLogout() {
			
		connect.SendRequest(Request.commands.LOGOUT, null);
		Reply re = connect.GetReply();
		
		if(re.getReturnType() == Reply.return_value.LOGOUT_VALID) {
			this.serverSays.setText("LOGOUT_VALID");
		} else {
			this.serverSays.setText("LOGOUT_FAILED");
		}
		
	}



	/**
	 * <b>메소드 :</b>
	 * <b>메소드 한글/영문 명을 여기에 기입합니다.</b>
	 * <p>
	 * <ul>
	 * <li>메소드에 대한 설명1
	 * <li>메소드에 대한 설명2
	 * </ul>
	 * </p>
	 * @since 2012. 6. 3.
	 */
	protected void procSearchAcount() {
		// TODO Auto-generated method stub
		
	}



	/**
	 * <b>메소드 :</b>
	 * <b>메소드 한글/영문 명을 여기에 기입합니다.</b>
	 * <p>
	 * <ul>
	 * <li>메소드에 대한 설명1
	 * <li>메소드에 대한 설명2
	 * </ul>
	 * </p>
	 * @since 2012. 6. 3.
	 */
	protected void procLogin() {
		String[] args = new String[2];
		args[0] = param1.getText().toString();
		args[1] = param2.getText().toString();
		
		connect.SendRequest(Request.commands.LOGIN, args);
		System.out.println("LOING REQUEST");
		
		Reply re = connect.GetReply();
		System.out.println("REPLY : " + re);
		
		if(re.getReturnType() == Reply.return_value.LOGIN_VALID) {
			this.serverSays.setText("LOGIN_VALID");
		} else {
			this.serverSays.setText("LOGIN_FAILED");
		}
		
	}
}